
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>  
#include <iostream>
#include <execinfo.h>

void fun_lib()
{
    std::cout<<"i am a lib fun"<<std::endl;

    int* p=NULL;
     *p = NULL;

    std::cout<<"i am a lib fun end"<<std::endl;

}




void WidebrightSegvHandler(int signum)  
{  
    void *array[30];  
    int size;  

    fprintf(stderr, "Error: signal received%d:\n", signum);

    signal(signum, SIG_DFL); /* Restore the default signal handler */  

    size = backtrace(array, 30);  
    if(size>2){
        void** ptr = &array[2] ;
        size -=2 ;
        backtrace_symbols_fd(ptr, size, 2);
    }else{
        backtrace_symbols_fd(array, size, 2);
    }


    raise(signum);
}  

int signal_catch()
{
    /* set the signal handle function */

#if 1

    if(SIG_ERR == signal(SIGHUP, WidebrightSegvHandler))
    {
        std::cout<<"SIGHUP signal setting failed"<<std::endl;
        return -1;
    }

    if(SIG_ERR == signal(SIGINT, WidebrightSegvHandler))
    {
        std::cout<<"SIGINT signal setting failed"<<std::endl;
        return -1;
    }

    if(SIG_ERR == signal(SIGQUIT, WidebrightSegvHandler))
    {
        std::cout<<"SIGQUIT signal setting failed"<<std::endl;
        return -1;
    }

    if(SIG_ERR == signal(SIGILL, WidebrightSegvHandler))
    {
        std::cout<<"SIGILL signal setting failed"<<std::endl;
        return -1;
    }

    if(SIG_ERR == signal(SIGTRAP, WidebrightSegvHandler))
    {
        std::cout<<"SIGTRAP signal setting failed"<<std::endl;
        return -1;
    }

    if(SIG_ERR == signal(SIGABRT, WidebrightSegvHandler))
    {
        std::cout<<"SIGABRT signal setting failed"<<std::endl;
        return -1;
    }

    if(SIG_ERR == signal(SIGIOT, WidebrightSegvHandler))
    {
        std::cout<<"SIGIOT signal setting failed"<<std::endl;
        return -1;
    }

    if(SIG_ERR == signal(SIGBUS, WidebrightSegvHandler))
    {
        std::cout<<"SIGBUS signal setting failed"<<std::endl;
        return -1;
    }

    if(SIG_ERR == signal(SIGFPE, WidebrightSegvHandler))
    {
        std::cout<<"SIGFPE signal setting failed"<<std::endl;
        return -1;
    }

    if(SIG_ERR == signal(SIGSEGV, WidebrightSegvHandler))
    {
        std::cout<<"SIGSEGV signal setting failed"<<std::endl;
        return -1;
    }

    if(SIG_ERR == signal(SIGPIPE, WidebrightSegvHandler))
    {
        std::cout<<"SIGPIPE signal setting failed"<<std::endl;
        return -1;
    }

    if(SIG_ERR == signal(SIGTERM, WidebrightSegvHandler))
    {
        std::cout<<"SIGTERM signal setting failed"<<std::endl;
        return -1;
    }

    if(SIG_ERR == signal(SIGTSTP, WidebrightSegvHandler))
    {
        std::cout<<"SIGTSTP signal setting failed"<<std::endl;
        return -1;
    }

    if(SIG_ERR == signal(SIGTTIN, WidebrightSegvHandler))
    {
        std::cout<<"SIGTTIN signal setting failed"<<std::endl;
        return -1;
    }

    if(SIG_ERR == signal(SIGTTOU, WidebrightSegvHandler))
    {
        std::cout<<"SIGTTOU signal setting failed"<<std::endl;
        return -1;
    }  

    if(SIG_ERR == signal(SIGSYS, WidebrightSegvHandler))
    {
        std::cout<<"SIGSYS signal setting failed"<<std::endl;
        return -1;
    }
#endif

    return 0;
}  